﻿using System;
using System.Windows;
using System.IO.IsolatedStorage;

namespace MyFloridaHouse.libSvOpenDistricts.StorageIsolated
{
    public delegate void IsolatedStorageDialogCompletedHandler(object sender, EventArgs e);

    public class IsolatedStorageIncreaseDialog
    {

        public static event IsolatedStorageDialogCompletedHandler IsolatedStorageDialogCompleted;


        public static void RaiseIsolatedStorageDialogCompleted(object sender)
        {
            if (IsolatedStorageDialogCompleted != null) { IsolatedStorageDialogCompleted(sender, new EventArgs()); }
        }

        //add space as necessary
        const long additionalSpaceMultiplier = (long)1.5;

        public void SaveToIsolatedStorageWithDialog(string fileDirectory, string fileName, bool isFileDataString, string fileDataString, byte[] fileData)
        {
            using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForSite())
            {
                Int64 spaceToAdd = fileData.Length;
                Int64 currAval = isf.AvailableFreeSpace;
                if (currAval < spaceToAdd)
                {
                    IsolatedStorageIncreaseDialogWindow dlg = new IsolatedStorageIncreaseDialogWindow();
                    dlg.fileName = fileName;
                    dlg.isFileDataString = isFileDataString;
                    dlg.fileDataString = fileDataString;
                    dlg.fileData = fileData;
                    dlg.fileDirectory = fileDirectory;
                    dlg.Title = "Request to Increase Local Storage by " + fileData.Length * additionalSpaceMultiplier / 1000 + " K bytes";
                    dlg.Show();
                    dlg.Closed += new EventHandler(onIncreaseIsolatedStrgDlgClosedEventHandler);
                }
                else
                {
                    bool result;
                    if (isFileDataString)
                    {
                        result = IsolatedStorageManager.SaveData(fileDataString, fileDirectory, fileName);
                    }
                    else
                    {
                        result = IsolatedStorageManager.SaveData(fileData, fileDirectory, fileName);
                    }
                    if (!result)
                    {
                        MessageBox.Show("Sorry, save failed");
                    }
                }
                isf.Dispose();
            }

        }

        private void onIncreaseIsolatedStrgDlgClosedEventHandler(object sender, EventArgs e)
        {
            IsolatedStorageIncreaseDialogWindow dlg = (IsolatedStorageIncreaseDialogWindow)sender;
            dlg.Closed -= new EventHandler(onIncreaseIsolatedStrgDlgClosedEventHandler);
            bool isIncreaseApproved = (bool)dlg.DialogResult;
            if (isIncreaseApproved)
            {
                using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForSite())
                {
                    try
                    {

                        isf.IncreaseQuotaTo((long)dlg.fileData.Length * additionalSpaceMultiplier + isf.Quota);
                        bool result = IsolatedStorageManager.SaveData(dlg.fileData, dlg.fileDirectory, dlg.fileName);
                        if (!result)
                        {
                            MessageBox.Show("Unable to store file in local isolated storage");
                        }
                    }
                    catch (IsolatedStorageException ex)
                    {
                        MessageBox.Show("Unable to store file in local isolated storage" + ex.Message);
                    }
                    isf.Dispose();
                }
                return;
            }

            RaiseIsolatedStorageDialogCompleted(this);
        }

    }
}
